#include <cmath>
#include <fstream>
#include <iostream>
using namespace std;

#include "hmm.h"

int main(int argc, char* argv[])
{
    Hmm hmm;
    hmm.loadProbs(argv[1]);
    string line;
    while( getline(cin,line) )
    {
        vector<unsigned int> seq;
        while( line.size() )
        {
            string word;
            string::size_type spaceidx = line.find(" ");
            if( spaceidx != string::npos )
            {
                word = line.substr(0, spaceidx);
                line = line.substr(spaceidx + 1, line.size() - spaceidx - 1);
            }
            else
            {
                word = line;
                line = "";
            }
            seq.push_back(hmm.getId(word));
        }
        line.clear();
        vector<unsigned int> path;
        hmm.viterbi2(path, seq);
        size_t i = 0;
        while( i < path.size())
        {
            string tmp = hmm.getStr(path[i]);
            if( tmp == "O" )
            {
                cout<<hmm.getStr(seq[i])<<" ";
                i++;
            }
            else if(tmp == "nt_U" || tmp == "ns_U" || tmp == "nr_U")
            {
                cout<<"["<<tmp.substr(0,2)<<" "<<hmm.getStr(seq[i])<<" ] ";
                i++;
            }
//            else if( tmp == "nr" )
//            {
//                cout<<"["<<tmp<<" ";
//                int j = i;
//                string tag = hmm.getStr(path[j]);
//                while(j < path.size() && tag == "nr")
//                {
//                    cout<<hmm.getStr(seq[j])<<" ";
//                    j++;
//                    if (j< path.size()) tag = hmm.getStr(path[j]);
//                }
//                if(j < path.size()) cout<<" "<<"] ";
//                i = j + 1;
//            }
            else if( tmp[tmp.size()-1] == 'B' )
            {
                tmp = tmp.substr(0,tmp.size()-2);
                cout<<"["<<tmp<<" ";
                cout<<hmm.getStr(seq[i])<<" ";
                int j = i + 1;
                if( j >=  path.size() ) 
                {
                    cout<<" ] ";
                    break;
                }
                string tag = hmm.getStr(path[j]);
                while(j < path.size() && tag[tag.size()-1] != 'L')
                {
                    cout<<hmm.getStr(seq[j])<<" ";
                    j++;
                    if (j< path.size()) tag = hmm.getStr(path[j]);
                }
                if(j < path.size()) cout<<hmm.getStr(seq[j])<<" "<<"] ";
                i = j + 1;
            }
            else
            {
                i++;
            }
        }
        cout<<endl;
    }
    cout<<endl;
    //hmm.reset();
}
